$(document).ready(function(){

	$("#add_data_component").click(function(){
		$("#data_component_panel").modal('hide');
		var componentUris = "";
		var count = 1;
		$("#data_component_table tbody").html("");
		var rows = $("<tbody></tbody>");
		$("#data_component_panel input[type='checkbox']").each(function(){
			var tr = $("<tr></tr>");
			if ($(this).prop("checked")){
				componentUris += $(this).val();
				componentUris += "/";
				var numTd = $("<td></td>").append(count++);
				var nameTd = $("<td></td>").append($(this).parent().text()); //获取label中的text
				tr.append(numTd).append(nameTd).appendTo(rows);
			}
		});
		$("#data_component_table tbody").html(rows.html());
		//名字为entity_data_area的input标签
		$("input[name='entity_data_area']").val(componentUris);
	});
	
	//删除
//	$("button[name='delete_equipment']").click(function(){
//		var uri = $(this).siblings('input').val();
//		var dpComponentUri = $("input[name='dpcUri']").val();
//		$.post('/ManEnv/busDesignPattern/delete',
//				{name:'equipment',equipmentUri:uri, dpcUri:dpComponentUri},
//				function(){
//					alert("success");
//				});
//		//找到tr,把整行删除
//		$(this).parent().parent().parent().remove();
//	});
	
	$("button[name='viewBusZone']").click(function(){
		var busZoneId = $(this).siblings('input').val();
		jQuery.ajax({
			type : 'POST',
			url : '/ManEnv/busZone/' + busZoneId,
			success : function(result) {
				$("#busZoneInfo").html("");
				for (i in result.entityZones){
					$("#busZoneInfo").append('<tr><td>' + result.entityZones[i].name + 
							'</td><td><span>'+ result.entityZones[i].size + 
							'</span></td></tr>');
				}
//				var undefineText = "undefined"
//				$("#equipment_info").text(result.equipment_info);
//				$("#bus_type_info").text(result.bus_type_info);
	//
//				if (result.equipment_number_info == -1)
//					$("#equipment_number_info").text(undefineText);
//				else
//					$("#equipment_number_info").text(result.equipment_number_info);
	//
//				$("#read_bus_processing_method_info").text(
//						result.read_bus_processing_method_info);
//				$("#bus_zone_info").text(result.bus_zone);
	//
//				if (result.read_frequency_info == -1)
//					$("#read_frequency_info").text(undefineText);
//				else
//					$("#read_frequency_info").text(result.read_frequency_info);
//				$("#write_bus_processing_method_info").text(
//						result.write_bus_processing_method_info);
	//
//				if (result.write_frequency_info == -1)
//					$("#write_frequency_info").text(undefineText);
//				else
//					$("#write_frequency_info").text(result.write_frequency_info);
			},
			error : function() {
				alert("error!");
			}
		});
	});
	
	//详细信息
	$("button[name='view_equipment']").click(function(){
		var dpcUri = $("input[name='dpcUri']").val();
		var uri = $(this).siblings('input').val();
//		var jsonStringList = [];
//		jsonStringList.push({dpiFileName:fileName});
//		jsonStringList.push({name:'equipment'});
//		jsonStringList.push({equipmentUri:uri});
         jQuery.ajax( {     
           type : 'POST',     
           contentType : 'application/json',
           data: JSON.stringify({dpcUri:dpcUri, name:'equipment', equipmentUri:uri}),
          // data: JSON.stringify(jsonStringList),
           url : '/ManEnv/busDesignPattern/info',   
           dataType : 'json',
           success : function(result) {
        	   var undefineText = "undefined"
        	   $("#equipment_info").text(result.equipment_info);
        	   $("#bus_type_info").text(result.bus_type_info);
        	  // if (result.entity_data_areas_info == "")
        		//   $("#entity_data_areas_info").text(undefineText);
        	   //else 
        		//   $("#entity_data_areas_info").text(result.entity_data_areas_info);
        	   
        	   //if (result.bus_data_area_info == -1)
        		//   $("#bus_data_area_info").text(undefineText);
        	  // else 
        		//   $("#bus_data_area_info").text(result.bus_data_area_info);
        	   
        	   if (result.equipment_number_info == -1)
        		   $("#equipment_number_info").text(undefineText);
        	   else 
        		   $("#equipment_number_info").text(result.equipment_number_info);
        	   
        	   $("#read_bus_init_method_info").text(result.read_bus_init_method_info);
        	   $("#read_bus_processing_method_info").text(result.read_bus_processing_method_info);
        	   $("#bus_zone_info").text(result.bus_zone);
        	   
        	  // if (result.read_frequency_info== -1)
        	//	   $("#read_frequency_info").text(undefineText);
        	 //  else 
        	//	   $("#read_frequency_info").text(result.read_frequency_info);
        	   
        	   //if (result.write_bus_processing_method_info== -1)
        	//	   $("#write_bus_processing_method_info").text(undefineText);
        	  // else 
        	   $("#write_bus_init_method_info").text(result.write_bus_init_method_info);
        	   $("#write_bus_processing_method_info").text(result.write_bus_processing_method_info);
        	   
        	 //  if (result.write_frequency_info== -1)
        	//	   $("#write_frequency_info").text(undefineText);
        	 //  else 
        	//	   $("#write_frequency_info").text(result.write_frequency_info);
        	   
        	  // $("#read_frequency_info").text(result.read_frequency_info);
        	   //$("#write_bus_processing_method_info").text(result.write_bus_processing_method_info);
        	   //$("#write_frequency_info").text(result.write_frequency_info);
           },     
           error : function() {     
             alert("error!");     
           }     
         });
	});
	
	
});

function deleteAgent(uri, deleteBtn){
	//var uri = $(deleteBtn).siblings('input').val();
	var dpComponentUri = $("input[name='dpcUri']").val();
	$.ajax({
		url : '/ManEnv/busDesignPattern/delete',
		type : 'POST',
		data: {name:'equipment',equipmentUri:uri, dpcUri:dpComponentUri},
		success : function(data) {
			if(data.result == "true"){
				alert("删除成功!");	
				$(deleteBtn).parents("tr").remove(); // 删除第一个tr元素
			}else{
				alert("删除失败!");
			}
		}
	});
}

function deleteBusZone(busZoneId, deleteBtn){
	//var id = $(deleteBtn).siblings('input').val();
	var projectId = $("input[name='projectId']").val();
	$.ajax({
		url : '/ManEnv/busZone/' + projectId + "/" + busZoneId,
		type : 'DELETE',
		success : function(data) {
			if(data.result == "true"){
				alert("删除成功!");	
				$(deleteBtn).parents("tr").remove(); // 删除第一个tr元素
			}else{
				alert("删除失败!");
			}
		}
	});
}

function viewBusZone(viewButton) {
	var busZoneId = $(viewButton).siblings('input').val();
	jQuery.ajax({
		type : 'POST',
		url : '/ManEnv/busZone/' + busZoneId,
		success : function(result) {
			for (i in result.entityZones){
				alert(i);
				alert(result.entityZones[i]);
			}
//			var undefineText = "undefined"
//			$("#equipment_info").text(result.equipment_info);
//			$("#bus_type_info").text(result.bus_type_info);
//
//			if (result.equipment_number_info == -1)
//				$("#equipment_number_info").text(undefineText);
//			else
//				$("#equipment_number_info").text(result.equipment_number_info);
//
//			$("#read_bus_processing_method_info").text(
//					result.read_bus_processing_method_info);
//			$("#bus_zone_info").text(result.bus_zone);
//
//			if (result.read_frequency_info == -1)
//				$("#read_frequency_info").text(undefineText);
//			else
//				$("#read_frequency_info").text(result.read_frequency_info);
//			$("#write_bus_processing_method_info").text(
//					result.write_bus_processing_method_info);
//
//			if (result.write_frequency_info == -1)
//				$("#write_frequency_info").text(undefineText);
//			else
//				$("#write_frequency_info").text(result.write_frequency_info);
		},
		error : function() {
			alert("error!");
		}
	});
	
}

function showBusZoneDeletePanel(busZoneId, deleteBtn){
    $("#delete-bus-zone-panel").modal({
        "show" : true,
        "backdrop" : false
    });
    $("#delete-bus-zone-btn").unbind("click");
    $("#delete-bus-zone-btn").click(function(){
    	deleteBusZone(busZoneId, deleteBtn);
    });
}

function showBusAgentDeletePanel(busZoneUri, deleteBtn){
    $("#delete-bus-agent-panel").modal({
        "show" : true,
        "backdrop" : false
    });
    $("#delete-bus-agent-btn").unbind("click");
    $("#delete-bus-agent-btn").click(function(){
    	deleteAgent(busZoneUri, deleteBtn);
    });
}
